package com.mes.md.controller;

import com.mes.common.pojo.MESResult;
import com.mes.md.service.ItemBomService;
import com.mes.md.service.ItemService;
import com.mes.md.service.ItemSipService;
import com.mes.md.service.ItemSopService;
import com.mes.pojo.md.Item;
import com.mes.pojo.md.MdProductBom;
import com.mes.pojo.md.MdProductSip;
import com.mes.pojo.md.MdProductSop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author hitsune
 * @Company kafu chino
 * @Description
 * @Time 2024/10/20 21:07
 */

@RestController
@RequestMapping("/item")
@CrossOrigin
public class ItemController {
    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemBomService itemBomService;

    @Autowired
    private ItemSipService itemSipService;

    @Autowired
    private ItemSopService itemSopService;


    @GetMapping("/query")
    public MESResult queryItemList(Item item){
        return MESResult.success(itemService.queryItemList(item));
    }

    @PostMapping("/add")
    public MESResult addItem(@RequestBody Item item){

        return MESResult.success(itemService.addItem(item));
    }

    @GetMapping("/queryItemById")
    public MESResult queryItemById(Long itemId){
        return MESResult.success(itemService.queryItemById(itemId));
    }

    @PostMapping("/update")
    public MESResult updateItem(@RequestBody Item item){
        return MESResult.success(itemService.updateItem(item));
    }
    @PostMapping("/deletes")
    public MESResult deleteItems(@RequestBody List<Item> items){
        return MESResult.success(itemService.deleteItems(items));
    }

    @GetMapping("/queryItemBomList")
    public MESResult queryItemBomList(Long itemId){
        return MESResult.success(itemBomService.queryItemBomList(itemId));
    }

    @PostMapping("/addItemToBom")
    public MESResult addItemToBom(@RequestBody List<Item> items){
        List<MdProductBom> itemBoms = new ArrayList<>();
        //将List<Item>中的数据与创造的List<ItemBom> itemBoms匹配
        items.forEach(item -> {
            MdProductBom itemBom = new MdProductBom();
            itemBom.setBomItemCode(item.getItemCode());
            itemBom.setBomItemId(item.getItemId());
            itemBom.setBomItemName(item.getItemName());
            itemBom.setBomItemSpec(item.getSpecification());
            itemBom.setBomItemStatus(item.getItemStatus());
            itemBom.setEnableFlag(item.getEnableFlag());
            itemBom.setItemId(item.getItemId());
            itemBom.setItemOrProduct(item.getItemOrProduct());
            itemBom.setQuantity(BigDecimal.valueOf(1));
            itemBom.setUnitOfMeasure(item.getUnitOfMeasure());
            itemBoms.add(itemBom);
        });
        System.out.println("itemBoms = " + itemBoms);
        return MESResult.success(itemBomService.addItemToBom(itemBoms));
    }

    @GetMapping("/selectItemBomById")
    public MESResult selectItemBomById(Long bomId){
        return MESResult.success(itemBomService.selectItemBomById(bomId));
    }

    @PostMapping("/itemBomEditDateSubmit")
    public MESResult itemBomEditDateSubmit(@RequestBody MdProductBom itemBom){
        return MESResult.success(itemBomService.updateItemBom(itemBom));
    }

    @GetMapping("/querySipByItemId")
    public MESResult querySipByItemId(Long itemId){
        return MESResult.success(itemSipService.querySipByItemId(itemId));
    }
    @GetMapping("/querySipById")
    public MESResult querySipById(Long sipId){
        return MESResult.success(itemSipService.querySipById(sipId));
    }

    @GetMapping("/queryProcessList")
    public MESResult queryProcessList(Long itemId){
        return MESResult.success(itemSipService.queryProcessList(itemId));
    }

    @PostMapping("/addSip")
    public MESResult addSip(@RequestBody MdProductSip mdProductSip){
        return MESResult.success(itemSipService.addSip(mdProductSip));
    }

    @GetMapping("/querySopByItemId")
    public MESResult querySopByItemId(Long itemId){
        return MESResult.success(itemSopService.querySopByItemId(itemId));
    }
    @GetMapping("/querySopById")
    public MESResult querySopById(Long sopId){
        return MESResult.success(itemSopService.querySopById(sopId));
    }

    @PostMapping("/addSop")
    public MESResult addSop(@RequestBody MdProductSop mdProductSop){
        return MESResult.success(itemSopService.addSop(mdProductSop));
    }
}
